﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ResidencyPlacement;
using System.IO;

namespace ResidencyPlacementWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Instantiate the writer 
            TextWriter _writer = new TextBoxStreamWriter(textBox1);
            // Redirect the out Console stream 
            Console.SetOut(_writer);

            CollectionViewSource studentViewSource = (CollectionViewSource)(this.FindResource("studentViewSource"));
            studentViewSource.Source = App.ResSystem.Students;
            CollectionViewSource hospitalViewSource = (CollectionViewSource)(this.FindResource("hospitalViewSource"));
            hospitalViewSource.Source = App.ResSystem.Hospitals;

            try
            {
                notAcceptedLabel.Content = App.ResSystem.NumberOfStudentsNotAcceptedAnywhere().ToString();
            }
            catch
            {

            }
        }

        private void addStudent_Click(object sender, RoutedEventArgs e)
        {
            AddStudent addStudent = new AddStudent();
            addStudent.Owner = this;
            addStudent.ShowDialog();

            if (addStudent.DialogResult == true)
            {
                Student s = new Student(addStudent.ID, addStudent.StudentName);
                try
                {
                    App.ResSystem.AddStudent(s);
                }
                catch
                {

                }
            }
        }

        private void addHospital_Click(object sender, RoutedEventArgs e)
        {
            AddHospital addHospital = new AddHospital();
            addHospital.Owner = this;
            addHospital.ShowDialog();

            if (addHospital.DialogResult == true)
            { 
                Hospital h = new Hospital(addHospital.ID, addHospital.Name, addHospital.Address, addHospital.NumberOfPostions);
                try
                {
                    App.ResSystem.AddHospital(h);
                }
                catch
                {

                }
            }
        }

        private void editExamMark_Click(object sender, RoutedEventArgs e)
        {
            EditExamMark editExamMark = new EditExamMark();
            Student selected = (Student)studentDataGrid.SelectedItem;
            editExamMark.Owner = this;
            editExamMark.NameLabel = selected.Name;
            editExamMark.Mark = selected.EntranceExamMark;
            editExamMark.ShowDialog();

            if (editExamMark.DialogResult == true)
            {
                try
                {
                    App.ResSystem.StudentChangeEntranceExamMark(selected, editExamMark.Mark);
                }
                catch
                {
                
                }
            }
        }

        private void viewApplicationsStudent_Click(object sender, RoutedEventArgs e)
        {
            ViewStudentApplications viewApplications = new ViewStudentApplications();
            Student selected = (Student)studentDataGrid.SelectedItem;
            viewApplications.Owner = this;
            viewApplications.Title = "Applications for " + selected.Name;
            viewApplications.Student = selected;
            viewApplications.Applications = selected.Applications;
            viewApplications.ShowDialog();
        }

        private void viewApplicationsHospital_Click(object sender, RoutedEventArgs e)
        {
            ViewHospitalApplications viewApplications = new ViewHospitalApplications();
            Hospital selected = (Hospital)hospitalDataGrid.SelectedItem;
            viewApplications.Owner = this;
            viewApplications.Title = "Applications for " + selected.Name;
            viewApplications.Hospital = selected;
            viewApplications.ShowDialog();
        }

        private void hospitalDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;
            if (dataGrid.SelectedItem != null)
            {
                Hospital selected = (Hospital)dataGrid.SelectedItem;
                try
                {
                    offeredAdmissionLabel.Content = App.ResSystem.NumberOfStudentsOfferedAdmissionByAHospital(selected).ToString();
                    positionsFilledLabel.Content = App.ResSystem.NumberOfPositionsFilledInAHospital(selected).ToString();
                }
                catch
                {

                }
            }
            else
            {
                offeredAdmissionLabel.Content = "";
                positionsFilledLabel.Content = "";
            }
        }

        private void studentDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;
            if (dataGrid.SelectedItem != null)
            {
                Student selected = (Student)dataGrid.SelectedItem;

                try
                {
                    appliedToLabel.Content = App.ResSystem.NumberOfPositionsStudentAppliedTo(selected).ToString();
                    interviewsAttendedLabel.Content = App.ResSystem.NumberOfInterviewsStudentAttended(selected).ToString();
                    admissionsRecievedLabel.Content = App.ResSystem.NumberOfAdmissionsStudentRecieved(selected).ToString();
                }
                catch
                {

                }  
            }
            else
            {
                button2.IsEnabled = false;
                appliedToLabel.Content = "";
                interviewsAttendedLabel.Content = "";
                admissionsRecievedLabel.Content = "";
            }
        }
    }
}
